package com.example.demo.mapper;

import com.example.demo.entity.CultureBackgroundDetail;
import com.example.demo.entity.Instrument;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CultureBackgroundDetailMapper {

    @Select("SELECT * FROM culture_background_detail_tb")
    List<CultureBackgroundDetail> findAll();

    @Select("SELECT * FROM culture_background_detail_tb WHERE title LIKE CONCAT('%', #{title}, '%')")
    List<CultureBackgroundDetail> findByTitle(@Param("title") String title);

    @Insert("INSERT INTO culture_background_detail_tb (culture_background_detail_id, title, type, content, image, view_count) " +
            "VALUES (#{culture_background_detail_id}, #{title}, #{type}, #{content}, #{image}, #{view_count})")
    @Options(useGeneratedKeys = true, keyProperty = "culture_background_detail_id")
    void insert(CultureBackgroundDetail cultureBackgroundDetail);

    @Update("UPDATE culture_background_detail_tb SET culture_background_detail_id = #{culture_background_detail_id}, " +
            "title = #{title}, type=#{type}, content = #{content}, image = #{image} ,view_count = #{view_count}, create_time = #{create_time}" +
            "WHERE culture_background_detail_id = #{culture_background_detail_id}")
    void update(CultureBackgroundDetail cultureBackgroundDetail);

    @Delete("DELETE FROM culture_background_detail_tb WHERE culture_background_detail_id = #{id}")
    void deleteById(@Param("id") Integer id);

    @Select("SELECT * FROM culture_background_detail_tb WHERE culture_background_detail_id = #{id}")
    CultureBackgroundDetail findById(@Param("id") Integer id);

    @Select("SELECT * FROM culture_background_detail_tb ORDER BY view_count DESC LIMIT 5")
    List<CultureBackgroundDetail> findHotBackground();

    // 更新浏览量
    @Update("UPDATE culture_background_detail_tb SET view_count = view_count + 1 WHERE culture_background_detail_id = #{id}")
    void incrementViewCount(Integer id);
}
